/**
 * jquery.oxeditor - jQuery plugin
 * build by freeall2008@gmail.com
 */
(function($) {
	if(!$.debug)$.debug=function(){};
	// XXX 插件扩展
	$.fn.extend({
		oxeditor : function(callback,opt) {
			var editors=[];
			this.each(function() {
				var editor=$(this);
				if ("textarea" === this.nodeName.toLowerCase()) {
					var editor=editors[editors.length] = preEditor(editor);
					if (!opt) {
						opt = $.fn.oxeditor.option;
					}
					editor.option(opt);
				}
				return editor;
			});
			return $(editors);
		}
	});
	// XXX 全局执行 (execCommand)
	$.extend({
		command : function(doc, cmd, ui, arg) {
			try {
				if (doc && doc.execCommand && cmd) {
					var re = doc.execCommand(cmd, ui, arg);
					if($.isDebug)
						$.debug("ExecCommand " + cmd + ": " + re);
				} else {
					if($.isDebug)
						$.debug("ExecCommand error: doc[" + doc + "] ui[" + ui
								+ "] arg[" + arg + "]");
				}
			} catch (e) {
				if($.isDebug)
						$.debug("ExecCommand doc[" + doc + "] ui[" + ui
								+ "] arg[" + arg + "]error: "+e.message);
			}
		}
	});
	// XXX 工具栏定义
	var OXPanel={
		addButton : function(html, className, callback, tooltip, text) {
			var doc=this.document;
			var button = html;
			if (className && className != "")
				button.addClass(className);
			if (tooltip && tooltip != "")
				button.attr("title", tooltip);
			if (text)button.html(text);
			// XXX 按键事件
			var name=button.attr("nodeName").toLowerCase();
			if("select"==name){
				button.change(function(){
					callback(doc,button);
				});
			} else if("a"==name){
				button.click(function(){
					callback(doc,button);
				});
			}
			this.append(button);
			return button;
		}
	};
	// XXX 编辑器定义
	var OXEditor={
		panels : {}
		,option : function(opt) {
			var re;
			var editor= this;
			if (opt) {
				re=$.each(opt, function(key,value) {
					var re;
					if(value){
						if($.isDebug)$.debug(key+" - ");
						var name=key;
						var text="";
						re=$.each(value,function(){
							return editor.button(name,this);
						});
						if($.isDebug)$.debug(text);
					}
					return re;
				});
			}
			return re;
		}
		,button : function(name,opt){
			var pl,re;
			if((pl=this.panels[name])||(pl=this.panel(name))){
				re=pl.addButton(	opt.html,
								opt.className,
								opt.callback,
								opt.title, null);
			}else{
				if($.isDebug)$.debug("Panel null: "+name);
			}
			return re;
		}
		,panel : function(name){
			var panel = $('<div></div>').addClass('panel');
			$.extend(panel,OXPanel);
			this.prepend(panel);
			this.panels[name]=panel;
			panel.document=this.document;
			return panel;
		}
		,oxhtml : function(){
			return this.document.body.innerHTML;
		}
	};
	// XXX 预处理
	function preEditor(editor) {
		var width =  editor.width();
		var height = editor.height();
		if($.isDebug)
			$.debug(width + " : " + height);
		// XXX 界面及内容
		var content = $.browser.msie ? "" : "<br>";
		content ="<P>Welcome to OXEditor !</P>";
		var html ='<html xmlns="http://www.w3.org/1999/xhtml"><head>'
				+ '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'
				+ '<style>'
				+ 'body {margin: 0px 0px;padding: 5px 5px;}</style>'
				+ '</head><body>' + content + '</body></html>';
		var frame = $('<iframe frameborder="0"></iframe>').attr("id",
				$(editor).attr("id") + "IFrame").attr("frameborder", 0);
		var div = $("<div></div>")
				// .append($("<div><!-- --></div>").css({clear : "both"}))
				.append($("<div></div>").addClass("iframe").append(frame));
		editor.hide().before(div);
		div.addClass("oxeditor");
		if($.isDebug)
			$.debug(div.width() + " : " + div.height());
		// XXX 编辑模式
		var htmleditor = frame[0].contentWindow.document;
		htmleditor.designMode = "on";
		htmleditor.open();
		htmleditor.write(html);
		htmleditor.close();
		htmleditor.body.contentEditable = true;
		// XXX 对象关联
		editor = div;
		editor.iframe = frame;
		editor.iframe.document = htmleditor;
		editor.document = htmleditor;
		$.extend(editor,OXEditor);
		return editor;
	}
})(jQuery);
